/**
 * 压缩解压缩常用方法.
 */
'use strict';
import * as zlib from  'zlib';

/**
 * 解压工具方法.
 * @param zipContent
 */
export function unzip (zipContent){

    return new Promise((resolve,reject)=>{
        var buffer = new Buffer(zipContent, 'base64');
        zlib.unzip(buffer, function(err, _buffer) {
            if (!err) {
                resolve(_buffer.toString())
            } else {
                console.error(err.stack);
                reject(err);
            }
        });
    })

}


export function  zip (contentStr){
    return new Promise((resolve, reject)=> {
        zlib.deflate(contentStr, function(err, buffer) {
            let  content =buffer.toString('base64');
            err?reject(err):resolve(content);
        });
    });
}


export function gzip (contentStr){
    return new Promise((resolve, reject)=>{
        zlib.gzip(contentStr, (err, buffer)=>{
            let  content =buffer.toString('base64');
            err?reject(err):resolve(content);
        });
    })

}

/**
 * 解压
 * @param data
 * @returns {Promise}
 */
export function unGzip (data){

    return new Promise((resolve, reject)=> {
        //console.info('开始解压数据:' + data.toString());
        let buf = new Buffer(data, 'base64');
        zlib.gunzip(buf, (error, result)=> {
            if (error) {
                console.error('解压数据出错,error=' + error);
                reject('解压数据出错,error=' + error);
            }
            resolve(result.toString());
        })
    })
}